<cfinclude template="header.cfm">
<cfif IsDefined("form.next")>

	<cfset ExpID="#createUUID()#"/><!---create a UniqueID, save it for later[not needed atm]--->


	<cfif not IsDefined("form.fNum")>
		<cfset fNum = "off" /> <!-- if the checkbox isn't ticked we need to define its value-->
	</cfif>

	<cfif not IsDefined("form.prac")>
		<cfset fNum = "off" /> <!-- if the checkbox isn't ticked we need to define its value-->
	</cfif>

	
	<!--- reSplit is a function from http://www.bennadel.com/blog/2058-RESplit-Splitting-Strings-With-Regular-Expressions-In-ColdFusion.htm --->
	<cffunction
      name="reSplit"
      access="public"
      returntype="array"
      output="false"
      hint="I split the given string using the given Java regular expression.">
       
      <!--- Define arguments. --->
      <cfargument
      name="regex"
      type="string"
      required="true"
      hint="I am the regular expression being used to split the string."
      />
       
      <cfargument
      name="value"
      type="string"
      required="true"
      hint="I am the string being split."
      />
       
      <!--- Define the local scope. --->
      <cfset var local = {} />
       
      <!---
      Get the split functionality from the core Java script. I am
      using JavaCast here as a way to alleviate the fact that I'm
      using  undocumented  functionality... sort of.
       
      The -1 argument tells the split() method to include trailing
      parts that are empty.
      --->
      <cfset local.parts = javaCast( "string", arguments.value ).split(
      javaCast( "string", arguments.regex ),
      javaCast( "int", -1 )
      ) />
       
      <!---
      We now have the individual parts; however, the split()
      method does not return a ColdFusion array - it returns a
      typed String[] array. We now have to convert that to a
      standard ColdFusion array.
      --->
      <cfset local.result = [] />
       
      <!--- Loop over the parts and append them to the results. --->
      <cfloop
      index="local.part"
      array="#local.parts#">
       
      <cfset arrayAppend( local.result, local.part ) />
       
      </cfloop>
       
      <!--- Return the result. --->
      <cfreturn local.result />
      </cffunction>
      
      <!--- Split this list, using the dash as our pattern. --->
      <cfset startdate = reSplit( "-", #sDate# ) />
      <cfset enddate = reSplit( "-", #eDate# ) />
      
	<!--- date insertion --->
	
		<!--- NOTE: coldfusion arrays begin indexing at 1 --->
		<cfset sDay = #startdate[1]#  />
		<cfset sMonth = #startdate[2]# />
		<cfset sYear = #startdate[3]# />
		<cfset eDay = #enddate[1]# />
		<cfset eMonth = #enddate[2]# />
		<cfset eYear = #enddate[3]# /> 

		<!--- fillers and practice blocks --->
		<cfif fNum eq "off">
			<cfset filler = 0>
		<cfelse>
			<cfset filler = 1>
		</cfif>

		<cfif prac eq "off">
			<cfset prac = 0>
		<cfelse>
			<cfset prac = 1>
		</cfif>
		
		<!--- okay? --->
		<cfoutput>
		<table border="1">

        <tr><th colspan=2>Experiment Details</th></tr>
        <tr>
        <td colspan=2>#Expname#</td></tr>
        <tr><td colspan=2>#desc#</td>
        </tr>
        <tr>
        <td >Maximum experiments </td><td>#maPar#</td></tr>
        <tr><td>Minimum experiments</td><td> #miPar#</td></td></tr>
        <tr><td>Start date: </td><td>#sDay#/#sMonth#/#sYear#</td></tr>
        <tr><td>End date: </td><td>#eDay#/#eMonth#/#eYear#</td></tr>
        <tr><td>Number of Conditions </td><td>#cNum#</td></tr>
        <!--- <tr><td>Conditions per Block </td><td>#cbNum#</td></tr> --->
        <tr><td>Fillers </td><td>#fNum#</td></tr>
        <tr><td>Questionnaires </td><td>#qNum#</td></tr>
		</table>
        
		Is this information correct?
		
		<!-- inserting form data into database -->
		<cfquery datasource="exp">	
		INSERT INTO experiment( userid, title, description, hasfiller, minpart, maxpart, startday, startmonth, startyear, endday, endmonth, endyear )
			VALUES( #session.id#, '#Expname#', '#desc#', #filler#, #miPar#, #maPar#, #sDay#, #sMonth#, #sYear#, #eDay#, #eMonth#, #eYear#) 
		</cfquery>
		
		<cfquery name="getNewID" datasource="exp">
			SELECT MAX(id) as newID
        	FROM experiment
        </cfquery>
        <cfset currID = getNewID.newID>
        <cfoutput></cfoutput>
        
        <!--- create this experiment's root directory --->
        	<!--- create stimuli directory --->	
        <cfif DirectoryExists("ecada\users\#session.id#\#currID#\")>
        <cfelse>
        	<cfdirectory action = "create" directory = "ecada\users\#session.id#\#currID#\" mode =777 >
        </cfif>
	
        <!---if an ethics file was defined, save it--->
        <cfif IsDefined("form.fileup")><!---check that the file field is defined--->
		<cfif FileExists("#fileup#")><!---check that the file about to be upload exists--->
			<cffile action="UPLOAD" 
				destination="ecada\users\#session.id#\#currID#\ethics.zip" 
				nameconflict="MAKEUNIQUE" 
				filefield="fileup">
				<!---upload file--->
				<!---
					technically this doesn't upload the file as it has already been
					Uploaded it merely moves a temp copy to your desired location
					kinda like the mv command in Linux
					[Ref http://blogs.adobe.com/cantrell/archives/2004/07/renaming_files.html]
				--->
				
			<!--- unzip directory --->
			<cfzip
        		action="unzip"
        		file="#ExpandPath( 'ecada\users\#session.id#\#currID#\ethics.zip' )#"
        		destination="#ExpandPath( 'ecada\users\#session.id#\#currID#\' )#"
        	/>
		</cfif>
		
		
		
	</cfif>	
		
        <cfheader statuscode="301" statustext="Moved Permanently">
        <cfheader name="Location" value="upload.cfm?cNum=#cNum#&currID=#currID#&prac=#prac#">
        <cfabort>

		<input type="button" value="Yes" onClick="window.location.href='upload.cfm?cNum=#cNum#&currID=#currID#'">
		<input type="button" value="No" onClick="window.location.href='ce.cfm'">

		<br />
		<br />
	</cfoutput>
	
<cfelse>
	<cfoutput>

				<cfinclude template="js/jquery.cfm"/>
				<!--
					this function for the datepicker was taken from the Jquery site
					http://jqueryui.com/demos/datepicker/
				-->
				<script>
					/* This function is for the date script*/
					$(function() {
						var dates = $( "##from, ##to" ).datepicker({
							defaultDate: "+1w",
							dateFormat: 'dd-mm-yy',/*jquery uses yy to represent a 4 digit year*/
							changeMonth: true,
							numberOfMonths: 1,/*modified from the example to only show one date box.*/
							onSelect: function( selectedDate ) {
								var option = this.id == "from" ? "minDate" : "maxDate",
									instance = $( this ).data( "datepicker" );
									date = $.datepicker.parseDate(
										instance.settings.dateFormat ||
										$.datepicker._defaults.dateFormat,
										selectedDate, instance.settings );
								dates.not( this ).datepicker( "option", option, date );
							}
						});
					});	
									
					$(document).ready(function() {
					/*tool tips*/
					/*jquery Qtips [Ref:http://www.craigsworks.com]*/					
					$('##ExpnameTT').qtip(
					 {
						content: 'This is the name of the experiment. Give your experiment a unique name to allow for easier navigation.', // Set the tooltip content to the current corner
						position: { corner: { tooltip: 'leftTop', target: 'rightBottom'}	},
						style: { border: { width: 5, radius: 10	}, padding: 10, textAlign: 'center',
							tip: true, // Give it a speech bubble tip with automatic corner detection
							name: 'light' // Style it according to the preset 'light' style
						}
					});
					
					$('##descTT').qtip(
					 {
						content: 'Describe the experiment here. A meaningful description will allow for easier navigation.', // Set the tooltip content to the current corner
						position: { corner: { tooltip: 'leftTop', target: 'rightBottom'}	},
						style: { border: { width: 5, radius: 10	}, padding: 10, textAlign: 'center',
							tip: true, // Give it a speech bubble tip with automatic corner detection
							name: 'light' // Style it according to the preset 'light' style
						}
					});
					
					$('##sDateTT').qtip(
					 {
						content: 'The date on which the experiment will begin.', // Set the tooltip content to the current corner
						position: { corner: { tooltip: 'leftTop', target: 'rightBottom'}	},
						style: { border: { width: 5, radius: 10	}, padding: 10, textAlign: 'center',
							tip: true, // Give it a speech bubble tip with automatic corner detection
							name: 'light' // Style it according to the preset 'light' style
						}
					});
					
					$('##eDateTT').qtip(
					 {
						content: 'The date on which the experiment will end. Participants will no longer be accepted after this date.', // Set the tooltip content to the current corner
						position: { corner: { tooltip: 'leftTop', target: 'rightBottom'}	},
						style: { border: { width: 5, radius: 10	}, padding: 10, textAlign: 'center',
							tip: true, // Give it a speech bubble tip with automatic corner detection
							name: 'light' // Style it according to the preset 'light' style
						}
					});
					
					
				
					$('##miParTT').qtip(
					 {
						content: 'The least amount of participants required before the experiment can be closed. Enter 0 if there is no minimum.', // Set the tooltip content to the current corner
						position: { corner: { tooltip: 'leftTop', target: 'rightBottom'}	},
						style: { border: { width: 5, radius: 10	}, padding: 10, textAlign: 'center',
							tip: true, // Give it a speech bubble tip with automatic corner detection
							name: 'light' // Style it according to the preset 'light' style
						}
					});
					
					$('##maParTT').qtip(
					 {
						content: 'The maximum number of participants an experiment requires before it can be closed. Enter 0 if there is no maximum.', // Set the tooltip content to the current corner
						position: { corner: { tooltip: 'leftTop', target: 'rightBottom'}	},
						style: { border: { width: 5, radius: 10	}, padding: 10, textAlign: 'center',
							tip: true, // Give it a speech bubble tip with automatic corner detection
							name: 'light' // Style it according to the preset 'light' style
						}
					});
					
					$('##cbNumTT').qtip(
					 {
						content: 'The number of conditions per block the experiment requires.', // Set the tooltip content to the current corner
						position: { corner: { tooltip: 'leftTop', target: 'rightBottom'}	},
						style: { border: { width: 5, radius: 10	}, padding: 10, textAlign: 'center',
							tip: true, // Give it a speech bubble tip with automatic corner detection
							name: 'light' // Style it according to the preset 'light' style
						}
					});
					
					$('##cNumTT').qtip(
					 {
						content: 'Number of conditions. These are the variables the experiment will be measured by.', // Set the tooltip content to the current corner
						position: { corner: { tooltip: 'leftTop', target: 'rightBottom'}	},
						style: { border: { width: 5, radius: 10	}, padding: 10, textAlign: 'center',
							tip: true, // Give it a speech bubble tip with automatic corner detection
							name: 'light' // Style it according to the preset 'light' style
						}
					});
					
					$('##tNumTT').qtip(
					 {
						content: 'The number of trials required for the experiment. Trials are the "questions" of the experiment.', // Set the tooltip content to the current corner
						position: { corner: { tooltip: 'leftTop', target: 'rightBottom'}	},
						style: { border: { width: 5, radius: 10	}, padding: 10, textAlign: 'center',
							tip: true, // Give it a speech bubble tip with automatic corner detection
							name: 'light' // Style it according to the preset 'light' style
						}
					});
					
					$('##qNumTT').qtip(
					 {
						content: 'Set the number of questionnaires. Questionnaires allow experimenters to ask questions of the participant and receive feedback. ', // Set the tooltip content to the current corner
						position: { corner: { tooltip: 'leftTop', target: 'rightBottom'}	},
						style: { border: { width: 5, radius: 10	}, padding: 10, textAlign: 'center',
							tip: true, // Give it a speech bubble tip with automatic corner detection
							name: 'light' // Style it according to the preset 'light' style
						}
					});
					
					$('##fileupTT').qtip(
					 {
						content: 'Upload a .zip file containing your ethics form. This will be displayed to participants when they begin the experiment.', // Set the tooltip content to the current corner
						position: { corner: { tooltip: 'leftTop', target: 'rightBottom'}	},
						style: { border: { width: 5, radius: 10	}, padding: 10, textAlign: 'center',
							tip: true, // Give it a speech bubble tip with automatic corner detection
							name: 'light' // Style it according to the preset 'light' style
						}
					});
					
					$('##subjectTT').qtip(
					 {
						content: 'Select whether your experiment will be between or within subjects. Between-subject experiments will require two sets of conditions.', // Set the tooltip content to the current corner
						position: { corner: { tooltip: 'leftTop', target: 'rightBottom'}	},
						style: { border: { width: 5, radius: 10	}, padding: 10, textAlign: 'center',
							tip: true, // Give it a speech bubble tip with automatic corner detection
							name: 'light' // Style it according to the preset 'light' style
						}
					});
					
					$('##fNumTT').qtip(
					 {
						content: "Tick the box to include fillers in your experiment. Fillers are short tasks that distract a participant's attention and impede their ability to memorize information.", // Set the tooltip content to the current corner
						position: { corner: { tooltip: 'leftTop', target: 'rightBottom'}	},
						style: { border: { width: 5, radius: 10	}, padding: 10, textAlign: 'center',
							tip: true, // Give it a speech bubble tip with automatic corner detection
							name: 'light' // Style it according to the preset 'light' style
						}
					});
					
					$('##cNumTT').qtip(
					 {
						content: "Enter the number of conditions your experiment will have. Each condition will require a set of stimuli.", // Set the tooltip content to the current corner
						position: { corner: { tooltip: 'leftTop', target: 'rightBottom'}	},
						style: { border: { width: 5, radius: 10	}, padding: 10, textAlign: 'center',
							tip: true, // Give it a speech bubble tip with automatic corner detection
							name: 'light' // Style it according to the preset 'light' style
						}
					});
					
					/*custom date validation, can't use the built in one as it can only check for American date standard. We must define a new validator for dates in the format dd-mm-yyyy and that is done with regular expressions*/
					$.validator.addMethod("pDate",
						function(value, element)
						{
							/*Regular Expression matches dd-mm-yyyy*/
							re = /^[0-3]?[0-9]-[01]?[0-9]-[12][90][0-9][0-9]$/;
							return value.match(re);
						},
						"Date is in incorrect format.");
						
					/*this method is for making sure that maximum participants is greater or equal to minimum participants*/
					$.validator.addMethod("gte",
						function(value, element, params)
						{
							if(value >=  $('##'+params).val())
							{
								return true;
							}
							else
							{
								return false;
							}
						},
						"This must be equal to or greater than the minimum number of participants.");
						
					/*this function is for form validation using jQuery. */
					$("##form1").validate({
						rules: {
								Expname: { required: true },
								desc: { required: true },
								sDate: { required: true, pDate: true },
								eDate: { required: true, pDate: true },
								miPar: { required: true, number: true },
								maPar: { required: true, number: true, /* gte: "miPar"*/},							
								cNum: { required: true, number: true },
								bNum: { required: true, number: true },
								cbNum: { required: true, number: true },
								qNum: { required: true, number: true },
								fileup: { accept: "zip"	}
							}
						});
					}); 
				</script>

				<form method = "post" id="form1" action = "ce.cfm" enctype="multipart/form-data">
					<table>
						<tr>
							<td id="ExpnameTT">Experiment Name	     :</td>
							<td><input type = "text" size = "40" name = "Expname"></td>
						</tr>
						<tr>
							<td id="descTT">Description          :</td>
							<td><textarea cols="40" rows="5" name="desc"></textarea></td>
						</tr>
						<tr>
							<td id="sDateTT">Start Date           :</td>
							<td><input type ="text" id="from" name="sDate"></td>
						</tr>
						<tr>
							<td id="eDateTT">End Date           :</td>
							<td><input type ="text" id="to" name="eDate"></td>
						</tr>
						<tr>
							<td id="miParTT">Minimum Participants :</td>
							<td><input type = "text"size = "10" id="miPar" name="miPar"></td>
						</tr>
						<tr>
							<td id="maParTT">Maximum Participants :</td>
							<td><input type = "text" size = "10" name="maPar"></td>
						</tr>
						
						
						<!--- for between subjects. would-like. <tr>
							<td id ="subjectTT">Subject :</td>
							<td><select name="subjectDropdown">
							<option value="Within">Within</option>
							<option value="Between">Between</option>
							</select></td>
						</tr>--->
						
						<tr>
							<td id="cNumTT">	Conditions       :</td>
							<td><input type	= "text" size = "10" name="cNum"></td>
						</tr>
						
						<!--- <tr>
							<td id="cbNumTT">	Conditions per block          :</td>
							<td><input type = "text" size = "10" name="cbNum"></td>
						</tr> --->
						<tr>
							<td id="fNumTT">	Include fillers          :</td>
							<td><input type = "checkbox" name="fNum"></td>
						</tr>
						<tr>
							<td id="pracTT">	Include practice block          :</td>
							<td><input type = "checkbox" name="prac"></td>
						</tr>
						<tr>
							<td id="qNumTT">	Questionnaires   :</td>
							<td><input type = "text" size = "10" name="qNum"></td>
						</tr>
						<tr>
							<td id="fileupTT">	Upload ethics form       :</td>
							<td><input type="File" name="fileup"/></td>
						</tr>
						<tr>
							<td><input type = "submit" name="next" value = "Next"></td>
						</tr>
					</table>
				</form>

	</cfoutput>
</cfif>
<cfinclude template="footer.cfm">
